﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Helpers;
using AtletIS.Shared.Forms;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;

namespace AtletIS.Shared.Forms
{
    public partial class frmDisciplineSelection : Form
    {
        private string nextForm;
        private DisciplineListTableAdapter taDisciplineList = new DisciplineListTableAdapter();

        // Constructor
        public frmDisciplineSelection(string nextForm)
        {
            InitializeComponent();

            this.nextForm = nextForm;
            this.Icon = Properties.Resources.AtletIS64;
        }

        // Form load
        private void frmDisciplineSelection_Load(object sender, EventArgs e)
        {
            try
            {
                switch (nextForm)
                {
                    case "frmChipViewer":
                        lblTitle.Text = "Chip Viewer";
                        break;

                    case "frmTrack":
                        lblTitle.Text = "Track";
                        break;

                    case "frmSpeaker":
                        lblTitle.Text = "Speaker";
                        break;

                    case "frmSector":
                        lblTitle.Text = "Sector";
                        break;

                    case "frmWindSpeed":
                        lblTitle.Text = "Wind Speed";
                        break;
                }

                DatabaseHelper.FillCompetitionsActive(this.aDbDataSet.Competitions);
                this.competitionsComboBox_SelectedIndexChanged(null, null);

                if (tvDisciplines.Nodes.Count > 0)
                    tvDisciplines.SelectedNode = tvDisciplines.Nodes[0];
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
        }

        // Competitions ComboBox
        private void competitionsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (competitionsComboBox.SelectedItem != null)
            {
                DataRowView drvCompetition = (DataRowView)competitionsComboBox.SelectedItem;
                Globals.ActiveCompetition = CompetitionHelper.ConvertToEntity(drvCompetition.Row);
                DisciplineListHelper.FillTreeView(tvDisciplines);
            }
        }

        // Koniec
        private void tsbClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        // Dalej
        private void tsbContinue_Click(object sender, EventArgs e)
        {
            if (tvDisciplines.SelectedNode == null) return;

            DataTable dtDisciplineList = DatabaseHelper.GetDisciplineListById(Convert.ToInt32(tvDisciplines.SelectedNode.Name)); 
            // taDisciplineList.GetById(Convert.ToInt32(tvDisciplines.SelectedNode.Name));
            if (dtDisciplineList.Rows.Count > 0)
            {
                Globals.ActiveDiscipline = DisciplineListHelper.ConvertToEntity(dtDisciplineList.Rows[0]);

                switch (nextForm)
                {
                    case "frmChipViewer":
                        if (Globals.ActiveDiscipline.DisciplineCode == Globals.LongRunCode
                            | Globals.ActiveDiscipline.DisciplineCode == Globals.MarathonCode)
                        {
                            frmWpf myFrmWpf = new frmWpf("ChipViewer", Globals.ActiveDiscipline);
                            myFrmWpf.Show();
                        }
                        else
                        {
                            MessageBox.Show("Pre túto disciplínu nie je možné spustiť Chip Viewer.", "Chip Viewer", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        break;

                    case "frmTrack":
                        if (Globals.ActiveDiscipline.DisciplineCode == Globals.SprintCode 
                            | Globals.ActiveDiscipline.DisciplineCode == Globals.LongRunCode
                            | Globals.ActiveDiscipline.DisciplineCode == Globals.MarathonCode)
                        {
                            frmTrack myFrmTrack = new frmTrack(Globals.ActiveDiscipline);
                            this.ShowInTaskbar = false;
                            myFrmTrack.ShowInTaskbar = true;
                            myFrmTrack.ShowDialog();
                            myFrmTrack.ShowInTaskbar = false;
                            this.ShowInTaskbar = true;
                        }
                        else
                        {
                            MessageBox.Show("Pre túto disciplínu nie je možné spustiť Track.", "Track", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        break;

                    case "frmSpeaker":
                        frmSpeaker myFrmSpeaker = new frmSpeaker(Globals.ActiveDiscipline, tvDisciplines.SelectedNode);
                        this.ShowInTaskbar = false;
                        myFrmSpeaker.ShowInTaskbar = true;
                        myFrmSpeaker.ShowDialog();
                        myFrmSpeaker.ShowInTaskbar = false;
                        this.ShowInTaskbar = true;

                        break;

                    case "frmSector":
                        if (Globals.ActiveDiscipline.DisciplineCode == Globals.ThrowsCode 
                            | Globals.ActiveDiscipline.DisciplineCode == Globals.HorJumpCode 
                            | Globals.ActiveDiscipline.DisciplineCode == Globals.VerJumpCode)
                        {
                            frmSector myFrmSector = new frmSector(Globals.ActiveDiscipline);
                            this.ShowInTaskbar = false;
                            myFrmSector.ShowInTaskbar = true;
                            myFrmSector.ShowDialog(this);
                            myFrmSector.ShowInTaskbar = false;
                            this.ShowInTaskbar = true;
                        }
                        else
                        {
                            MessageBox.Show("Pre túto disciplínu nie je možné spustiť Sector.", "Sector", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        break;

                    case "frmWindSpeed":
                        if (Globals.ActiveDiscipline.DisciplineCode == Globals.HorJumpCode)
                        {
                            frmWindSpeed myFrmWindSpeed = new frmWindSpeed(Globals.ActiveDiscipline);
                            this.ShowInTaskbar = false;
                            myFrmWindSpeed.ShowInTaskbar = true;
                            myFrmWindSpeed.ShowDialog();
                            myFrmWindSpeed.ShowInTaskbar = false;
                            this.ShowInTaskbar = true;
                        }
                        else
                        {
                            MessageBox.Show("Pre túto disciplínu nie je možné spustiť Wind Speed.", "Wind Speed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        break;
                }
            }
        }

        // Otvori Startovnu listinu
        private void tsmiStartListGraphics_Click(object sender, EventArgs e)
        {
            DataTable dtDisciplineList = DatabaseHelper.GetDisciplineListById(Convert.ToInt32(tvDisciplines.SelectedNode.Name)); // taDisciplineList.GetById(Convert.ToInt32(tvDisciplines.SelectedNode.Name));
            if (dtDisciplineList.Rows.Count > 0)
            {
                Globals.ActiveDiscipline = DisciplineListHelper.ConvertToEntity(dtDisciplineList.Rows[0]);
                frmWpf myFrmWpf = new frmWpf("StartList", Globals.ActiveDiscipline);
                myFrmWpf.Show();
            }
        }

        // Otvori Vysledky
        private void tsmiResultsGraphics_Click(object sender, EventArgs e)
        {
            DataTable dtDisciplineList = DatabaseHelper.GetDisciplineListById(Convert.ToInt32(tvDisciplines.SelectedNode.Name)); // taDisciplineList.GetById(Convert.ToInt32(tvDisciplines.SelectedNode.Name));
            if (dtDisciplineList.Rows.Count > 0)
            {
                Globals.ActiveDiscipline = DisciplineListHelper.ConvertToEntity(dtDisciplineList.Rows[0]);
                frmWpf myFrmWpf = new frmWpf("Results", Globals.ActiveDiscipline);
                myFrmWpf.Show();
            }
        }







    }
}
